package models

import (
	"time"
	"user_srv/basic/config"
)

// LocationHistory 位置历史记录表
type LocationHistory struct {
	Id        uint64    `gorm:"column:id;type:bigint UNSIGNED;primaryKey;not null;autoIncrement;" json:"id"`
	UserId    uint64    `gorm:"column:user_id;type:bigint UNSIGNED;index;not null;comment:用户ID;" json:"user_id"`
	Latitude  float64   `gorm:"column:latitude;type:decimal(10,7);not null;comment:纬度;" json:"latitude"`
	Longitude float64   `gorm:"column:longitude;type:decimal(10,7);not null;comment:经度;" json:"longitude"`
	Accuracy  float64   `gorm:"column:accuracy;type:decimal(8,2);comment:精度(米);default:NULL;" json:"accuracy"`
	Source    string    `gorm:"column:source;type:varchar(20);comment:来源;default:NULL;" json:"source"`
	Address   string    `gorm:"column:address;type:varchar(200);comment:地址;default:NULL;" json:"address"`
	City      string    `gorm:"column:city;type:varchar(50);comment:城市;default:NULL;" json:"city"`
	District  string    `gorm:"column:district;type:varchar(50);comment:区县;default:NULL;" json:"district"`
	Province  string    `gorm:"column:province;type:varchar(50);comment:省份;default:NULL;" json:"province"`
	CreatedAt time.Time `gorm:"column:created_at;type:datetime(3);not null;default:CURRENT_TIMESTAMP(3);" json:"created_at"`
	UpdatedAt time.Time `gorm:"column:updated_at;type:datetime(3);not null;default:CURRENT_TIMESTAMP(3);" json:"updated_at"`
	DeletedAt time.Time `gorm:"column:deleted_at;type:datetime(3);default:NULL;" json:"deleted_at"`
}

func (LocationHistory) TableName() string {
	return "location_history"
}

func (lh *LocationHistory) GetByUserId(userId uint64, limit int) ([]LocationHistory, error) {
	var history []LocationHistory
	err := config.DB.Where("user_id = ?", userId).
		Order("created_at DESC").
		Limit(limit).
		Find(&history).Error
	return history, err
}

// Create 创建位置历史记录
func (lh *LocationHistory) Create() error {
	return config.DB.Create(&lh).Error
}

// GeoFence 地理围栏表
type GeoFence struct {
	Id        uint64    `gorm:"column:id;type:bigint UNSIGNED;primaryKey;not null;autoIncrement;" json:"id"`
	UserId    uint64    `gorm:"column:user_id;type:bigint UNSIGNED;index;not null;comment:用户ID;" json:"user_id"`
	Name      string    `gorm:"column:name;type:varchar(100);not null;comment:围栏名称;" json:"name"`
	CenterLat float64   `gorm:"column:center_lat;type:decimal(10,7);not null;comment:中心纬度;" json:"center_lat"`
	CenterLng float64   `gorm:"column:center_lng;type:decimal(10,7);not null;comment:中心经度;" json:"center_lng"`
	Radius    float64   `gorm:"column:radius;type:decimal(8,2);not null;comment:半径(米);" json:"radius"`
	Status    int8      `gorm:"column:status;type:tinyint(1);default:1;comment:状态 1-启用 0-禁用;" json:"status"`
	CreatedAt time.Time `gorm:"column:created_at;type:datetime(3);not null;default:CURRENT_TIMESTAMP(3);" json:"created_at"`
	UpdatedAt time.Time `gorm:"column:updated_at;type:datetime(3);not null;default:CURRENT_TIMESTAMP(3);" json:"updated_at"`
	DeletedAt time.Time `gorm:"column:deleted_at;type:datetime(3);default:NULL;" json:"deleted_at"`
}

func (GeoFence) TableName() string {
	return "geo_fence"
}

func (gf *GeoFence) GetByUserId(userId uint64) ([]GeoFence, error) {
	var fences []GeoFence
	err := config.DB.Where("user_id = ? AND status = 1", userId).
		Order("created_at DESC").
		Find(&fences).Error
	return fences, err
}

// Create 创建地理围栏
func (gf *GeoFence) Create() error {
	return config.DB.Create(&gf).Error
}

// LocationStats 位置统计表
type LocationStats struct {
	Id                uint64    `gorm:"column:id;type:bigint UNSIGNED;primaryKey;not null;autoIncrement;" json:"id"`
	UserId            uint64    `gorm:"column:user_id;type:bigint UNSIGNED;index;not null;comment:用户ID;" json:"user_id"`
	TotalLocations    int       `gorm:"column:total_locations;type:int;default:0;comment:总位置数;" json:"total_locations"`
	ActiveDays        int       `gorm:"column:active_days;type:int;default:0;comment:活跃天数;" json:"active_days"`
	FrequentAreas     int       `gorm:"column:frequent_areas;type:int;default:0;comment:常去区域数;" json:"frequent_areas"`
	AverageAccuracy   float64   `gorm:"column:average_accuracy;type:decimal(8,2);default:0;comment:平均精度;" json:"average_accuracy"`
	LastActiveDate    time.Time `gorm:"column:last_active_date;type:date;default:NULL;comment:最后活跃日期;" json:"last_active_date"`
	CreatedAt         time.Time `gorm:"column:created_at;type:datetime(3);not null;default:CURRENT_TIMESTAMP(3);" json:"created_at"`
	UpdatedAt         time.Time `gorm:"column:updated_at;type:datetime(3);not null;default:CURRENT_TIMESTAMP(3);" json:"updated_at"`
}

func (LocationStats) TableName() string {
	return "location_stats"
}

func (ls *LocationStats) GetByUserId(userId uint64) error {
	return config.DB.Where("user_id = ?", userId).First(&ls).Error
}

// Create 创建位置统计记录
func (ls *LocationStats) Create() error {
	return config.DB.Create(&ls).Error
}

// Update 更新位置统计记录
func (ls *LocationStats) Update() error {
	return config.DB.Save(ls).Error
}

// LocationEvent 位置事件表
type LocationEvent struct {
	Id        uint64    `gorm:"column:id;type:bigint UNSIGNED;primaryKey;not null;autoIncrement;" json:"id"`
	UserId    uint64    `gorm:"column:user_id;type:bigint UNSIGNED;index;not null;comment:用户ID;" json:"user_id"`
	EventType string    `gorm:"column:event_type;type:varchar(50);not null;comment:事件类型;" json:"event_type"`
	Latitude  float64   `gorm:"column:latitude;type:decimal(10,7);not null;comment:纬度;" json:"latitude"`
	Longitude float64   `gorm:"column:longitude;type:decimal(10,7);not null;comment:经度;" json:"longitude"`
	Address   string    `gorm:"column:address;type:varchar(200);comment:地址;default:NULL;" json:"address"`
	Data      string    `gorm:"column:data;type:text;comment:事件数据;default:NULL;" json:"data"`
	CreatedAt time.Time `gorm:"column:created_at;type:datetime(3);not null;default:CURRENT_TIMESTAMP(3);" json:"created_at"`
}

func (LocationEvent) TableName() string {
	return "location_event"
}

func (le *LocationEvent) Create() error {
	return config.DB.Create(&le).Error
}

func (le *LocationEvent) GetByUserId(userId uint64, eventType string, limit int) ([]LocationEvent, error) {
	var events []LocationEvent
	query := config.DB.Where("user_id = ?", userId)
	if eventType != "" {
		query = query.Where("event_type = ?", eventType)
	}
	err := query.Order("created_at DESC").
		Limit(limit).
		Find(&events).Error
	return events, err
}
